package csu.web.credit_bank.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import csu.web.credit_bank.mapper.ItemExchangeMapper;
import csu.web.credit_bank.pojo.ItemExchange;
import csu.web.credit_bank.pojo.ShopItem;
import csu.web.credit_bank.pojo.vo.ShopItemHistory;
import csu.web.credit_bank.pojo.vo.UserShopItem;
import csu.web.credit_bank.service.ImageService;
import csu.web.credit_bank.service.ShopItemService;
import csu.web.credit_bank.mapper.ShopItemMapper;
import csu.web.credit_bank.utils.ImagePath;
import csu.web.credit_bank.utils.Result;
import csu.web.credit_bank.utils.UUIDManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
* @author Administrator
* @description 针对表【shop_item】的数据库操作Service实现
* @createDate 2025-06-30 15:49:40
*/
@Service
public class ShopItemServiceImpl extends ServiceImpl<ShopItemMapper, ShopItem>
    implements ShopItemService{
    @Autowired
    private ShopItemMapper shopItemMapper;
    @Autowired
    private ImageService imageService;
    @Autowired
    private ItemExchangeMapper itemExchangeMapper;
    @Autowired
    private UUIDManager uuidManager;

    @Override
    public Result addNewShopItem(ShopItem shopItem, MultipartFile file) {
        // 检查文件是否为空

        String path=imageService.saveImage(file);
        shopItem.setImage(path);
        shopItem.setId(uuidManager.getUUID());
        shopItemMapper.insert(shopItem);
        return Result.success(shopItem);
    }
    @Override
    public Result getShopItems() {
        return Result.success(shopItemMapper.selectList(null));
    }
    @Override
    public Result shopItem(UserShopItem userShopItem, String item_id) {
        //查看该用户是否已经具有改头像
        List<ItemExchange> itemExchangeList = itemExchangeMapper.selectList(new QueryWrapper<ItemExchange>().eq("user_id", userShopItem.getUser_id()));
        for(ItemExchange itemExchange:itemExchangeList){
            if(itemExchange.getItemId().equals(item_id)){
                return Result.failure("您已经拥有该头像");
            }
        }






        ShopItem shopItem=shopItemMapper.selectById(item_id);
        shopItem.setInventory(shopItem.getInventory()-userShopItem.getNum());
        shopItemMapper.updateById(shopItem);
        ItemExchange itemExchange = new ItemExchange();
        itemExchange.setItemId(item_id);
        itemExchange.setUserId(userShopItem.getUser_id());
        itemExchange.setDate(new Date());
        itemExchange.setEffective(1);
        itemExchangeMapper.insert(itemExchange);
        return Result.success("兑换成功");
    }
    @Override
    public Result getItemExchanges(String user_id) {
        List<ShopItemHistory> shopItemHistoryList=new ArrayList<>();
        List<ItemExchange> itemExchangeList = itemExchangeMapper.selectList(new QueryWrapper<ItemExchange>().eq("user_id", user_id));
        for(ItemExchange itemExchange:itemExchangeList){
            ShopItem shopItem=shopItemMapper.selectById(itemExchange.getItemId());
            ShopItemHistory shopItemHistory=new ShopItemHistory();
            shopItemHistory.setItem_id(itemExchange.getItemId());
            shopItemHistory.setImageUrl(shopItem.getImage());
            shopItemHistory.setTitle(shopItem.getTitle());
            shopItemHistory.setDate(itemExchange.getDate());
            shopItemHistory.setType(shopItem.getType());
            shopItemHistory.setIsEffective(itemExchange.getEffective());
            shopItemHistory.setDescription(shopItem.getDescription());
            shopItemHistoryList.add(shopItemHistory);
        }
        return Result.success(shopItemHistoryList);
    }

    @Override
    public Result updateItemExchange(UserShopItem userShopItem, String item_id) {
        //判断商品是否是头像框
        ShopItem shopItem=shopItemMapper.selectOne(new QueryWrapper<ShopItem>().eq("id", item_id));
        if(shopItem.getType()==2){
            //查询该用户是否有该头像框
            List<ItemExchange> itemExchangeList = itemExchangeMapper.selectList(new QueryWrapper<ItemExchange>()
                   .eq("user_id", userShopItem.getUser_id())); //获取用户的所有商品

           List<ShopItem> shopItemList=shopItemMapper.selectList(new QueryWrapper<ShopItem>().eq("type",2));//获取所有的头像框商品

            //将用户具有的头像框的商品的effective设置为1
            for(ItemExchange itemExchange:itemExchangeList){
                String itemId=itemExchange.getItemId();//获得用户的商品id
                //判断该商品是否是头像框
                for(ShopItem shopItem1:shopItemList){
                    if(shopItem1.getId().equals(itemId)){
                        itemExchange.setEffective(1);
                        itemExchangeMapper.update(itemExchange,new QueryWrapper<ItemExchange>().eq("item_id", itemId).eq("user_id", userShopItem.getUser_id()));
                        break;
                    }
                }
            }
            ItemExchange itemExchange=itemExchangeMapper.selectOne(new  QueryWrapper<ItemExchange>().eq("item_id", item_id).eq("user_id", userShopItem.getUser_id()));
            itemExchange.setEffective(0);
            itemExchangeMapper.update(itemExchange,new QueryWrapper<ItemExchange>().eq("user_id", userShopItem.getUser_id()).eq("item_id", item_id));

            return Result.success(shopItem);
        }
        else{
            // 查询满足条件的记录列表
            List<ItemExchange> itemExchangeList = itemExchangeMapper.selectList(new QueryWrapper<ItemExchange>()
                    .eq("user_id", userShopItem.getUser_id())
                    .eq("item_id", item_id)
                    .eq("effective", 1));
            // 检查列表是否为空
            if (itemExchangeList.isEmpty()) {
                return Result.failure("使用失败");
            }
            // 获取第一条记录
            ItemExchange itemExchange = itemExchangeList.get(0);
            itemExchange.setEffective(0);
            itemExchange.setDate(new Date());
            // 更新第一条满足条件的记录
            if(itemExchangeList.size()==1){
                itemExchangeMapper.update(itemExchange,new QueryWrapper<ItemExchange>()
                        .eq("user_id", userShopItem.getUser_id())
                        .eq("item_id", item_id));
                return Result.success("使用成功");
            }
            itemExchangeMapper.update(itemExchange, new QueryWrapper<ItemExchange>()
                    .eq("user_id", userShopItem.getUser_id())
                    .eq("item_id", item_id)
                    .last("LIMIT 1"));
            return Result.success("使用成功");
        }



    }
}




